package cn.edu.seu.herald.meq;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MorningExerciseQuery {
	
	private static final String POST_URL =
			"http://58.192.114.239:8080/studentLogin.do";
	
	private static final String INFO_URL =
			"http://58.192.114.239:8080/studentQueryListChecks.do?method=listChecks";
	
	private String _username;
	
	private String _password;
	
	private int _result = -1;
	
	public MorningExerciseQuery(String username, String password) {
		_username = username;
		_password = password;
	}
	
	public void execute() throws QueryFailure {
		try {
			String sessionCookie = step1(_username, _password);
			String htmlCode = step2(sessionCookie);
			_result = step3(htmlCode);
		} catch (Exception e) {
			throw new QueryFailure();
		}
	}
	
	public int getResult() {
		return _result;
	}
	
	private String step1(String username, String password) throws IOException {
		URL url = new URL(POST_URL);
    	HttpURLConnection httpurlconnection =
    			(HttpURLConnection) url.openConnection();
		httpurlconnection.setDoOutput(true);
		httpurlconnection.setRequestMethod("POST");
		
		StringBuilder builder = new StringBuilder();
		builder.append("xh=")
			.append(username)
			.append("&mm=")
			.append(password)
			.append("&method=login");
		
		String message = builder.toString();
		
		httpurlconnection.getOutputStream().write(message.getBytes());
		httpurlconnection.getOutputStream().flush();
		httpurlconnection.getOutputStream().close();
		
		httpurlconnection.getInputStream();
		
		String setCookie = httpurlconnection.getHeaderField("Set-Cookie");  
        String cookie = setCookie.substring(0, setCookie.indexOf(";"));
        return cookie;
	}
	
	private String step2(String cookie) throws IOException {
		URL url = new URL(INFO_URL);
    	HttpURLConnection httpurlconnection =
    			(HttpURLConnection) url.openConnection();
		httpurlconnection.setRequestMethod("GET");
		httpurlconnection.setRequestProperty("Cookie", cookie);
		
		InputStream in = httpurlconnection.getInputStream();
		InputStreamReader reader = new InputStreamReader(in);

		StringBuilder builder = new StringBuilder();
		while (true) {
			int c = reader.read();
			if (c <= 0) {
				break;
			}
			builder.append((char) c);
			if (!reader.ready()) {
				break;
			}
		}
		String htmlCode = builder.toString();
		return htmlCode.trim();
	}
	
	private int step3(String htmlCode) throws QueryFailure {
		String headTag = "<td class=\"Content_Form\">";
		Pattern pattern = Pattern.compile(
				 headTag +
				"[0-9]*</td>[\\s]*</tr></table></td></tr></table>");
		Matcher matcher1 = pattern.matcher(htmlCode);
		if (matcher1.find()) {
			String matched = matcher1.group();
			Pattern numPattern = Pattern.compile("(\\d+)");
			Matcher matcher2 = numPattern.matcher(matched);
			if (matcher2.find()) {
				return new Integer(matcher2.group());
			}
		}
		throw new QueryFailure();
	}

}
